
import { areaOptions } from "../utils/area"

class AreaStore {
  getAllProvinceFromLocal(params: {
    callBack: (res) => void
  }) {
    const { callBack } = params
    let temArr: any[] = []
    areaOptions.map(province => {
      temArr.push({
        value: province.value,
        label: province.label
      })
    })
    callBack && callBack(temArr)
  }

  /**
   * 获取所有市区信息
   */
  getAllCityFromLocal(params: {
    index?: number, // 省索引,默认为-1，取全部市
    callBack: (res) => void
  }) {
    const { index,callBack } = params
    let temArr: any[] = []

    if(index === -1){
      areaOptions.map(province => {
        province.children.map(city => {
          temArr.push({
            value: city.value,
            label: city.label
          })
        })
      })
    }else {
      temArr = areaOptions[index]['children']
    }
    callBack && callBack(temArr)
  }

  /**
   * 获取所有区信息
   */
  getAllDistractFromLocal(params: {
    indexProvince?: number,
    indexCity?: number, // 市索引,默认为-1，取全部市区
    callBack: (res) => void
  }) {
    const { indexProvince,indexCity,callBack } = params
    let temArr: any[] = []
    if(indexCity===-1){
      areaOptions.map(province => {
        province.children.map(city => {
          city.children.map(distract => {
            temArr.push({
              value: distract.value,
              label: distract.label
            })
          })
        })
      })
    }else {
      temArr = areaOptions[indexProvince]['children'][indexCity]['children']
    }
    callBack && callBack(temArr)
  }
}

export default new AreaStore()

